#!/usr/bin/python -tt


######### standard library imports ###########################################

import sys
import logging
import logging.config
import os


from optparse import OptionParser


######### project library imports ############################################
import settings

import readcorsika
import absorb_photons
import reflector   

import turnIntoZenit
import photonDetection

scriptpath = os.path.dirname(os.path.realpath(__file__))
logging.config.fileConfig(scriptpath+'/logging.conf')
logger = logging.getLogger('simulation_root')


parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
    help="write report to FILE", metavar="FILE")
parser.add_option("-p", "--plot",
    action="store_true", dest="create_plots", default=False,
    help="create some informative debugging plots")
options, args = parser.parse_args()
if options.filename is None:
    print 'Please submit a filename. "', sys.argv[0],'-h" for help'
    sys.exit(1)


##############################################################################
############# Here everything starts #########################################

thedict = {}

thedict = settings.put_settings_into_dict( thedict )

thedict['eventData'] = {}
for i in range(100):
    thedict['eventData'][i]= {}


thedict = photonDetection.addNsbAndDarkNoise( thedict, 
        noiseDictname='PhotonNoiseInfo', 
        outDictname='eventData')

thedict = photonDetection.simulateOpticalCrosstalk( thedict, 
        eventdictname='eventData',
        hitdictnames = ['NoiseHits'],
        outdictname = 'AllHitsWithWeights')

#------------ Adc simulation, i.e. rawdata generation

        ## This is the function, which eats most of the time ##
photonDetection.MakeAndSaveAdcData( thedict, filename=options.filename+'.fits')

logging.info("the end.")    


############# This is the end my friend ######################################
##############################################################################

if options.create_plots:
    readcorsika.make_plots(thedict)
    reflector.make_plots(thedict)


    
    
    
    
    

